如何连线便携式无线打印机

相关框架:PrinterDispatch.framework

PTDispatcher通讯调度员:

用于提供统一的BLE和 WiFi 外部接口

相关类:PTDispatcher

WiFi 连接接口:

7
1
#pragma mark PTNetWork
2
​
3
// 获取当前 Wifi 的路由信息
4
- (NSDictionary *)getRouterInfo;
5
​
6
// 通过 ip 地址连接 wifi
7
- (void)connectWifi:(NSString *)ip;

Bluetooth 4.0 (BLE) 接口:

1、扫描蓝牙

5
1
 如何获取扫描出来的蓝牙设备:任选一种
2
 代理方法:记得设置 BLE 代理。
3
  - (void)didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;
4
 Block:
5
  - (void)scanBluetooth:(PTDispatchDiscoverPeripheralBlock)discoverBlock;

2、连接指定蓝牙

10
1
 如何获取连接状态:任选一种
2
 注册通知:不推荐。PTDispatchModeChangeToBLE
3
 代理方法:推荐。 记得设置 BLE 代理。
4
 - (void)didConnectPeripheral:(CBPeripheral *)peripheral
5
  Block:推荐。
6
 - (void)connectBluetooth:(CBPeripheral *)peripheral
7
                   notify:(BOOL)notify
8
                  success:(PTDispatchConnectSuccessBlock)successBlock
9
                  failure:(PTDispatchConnectFailureBlock)failureBlock
10
              canSendData:(PTDispatchCanSendDataBlock)canSendBlock

3、发送数据给打印机

6
1
 /** 单纯发送数据给打印机 */
2
 - (void)sendData:(NSData *)data;
3
 /** 发送数据,并等待打印机返回值 */
4
 - (void)sendData:(NSData *)data returnData:(PTDispatchReceiveBlock)receiveBlock;
5
 /** 发送数据,并等待成功发送结束,或者发送失败原因 */
6
 - (void)sendData:(NSData *)data success:(PTDispatchSendSuccessBlock)successBlock disconnect:(PTDispatchDisconnectBlock)disconnectBlock;

4、断开连接

3
1
 /** 断开连接蓝牙 */
2
 - (void)disConnectBluetooth;
3
 - (void)disConnectBluetooth:(PTDispatchDisconnectBlock)disconnectBlock;

断开当前连接:

2
1
// 取消当前连接(Wifi 或者 BLE)
2
- (void)disConnectCurrentLine;

打印机的连接状态:

12
1
typedef NS_ENUM(NSInteger, PTDispatchMode) {
2
    
3
    PTDispatchModeOffline = 0, //离线模式
4
    PTDispatchModeBLE, //蓝牙4.0模式
5
    PTDispatchModeWiFi, //WiFi 模式
6
};
7
​
8
// 当前通讯调度模式
9
@property(nonatomic,assign) NSInteger currentDispatchMode;
10
​
11
// 当前连接的蓝牙
12
@property(strong,nonatomic,readwrite) CBPeripheral *bluetoothConnected;

PTDispatchProtocol 协议

11
1
@protocol PTDispatchProtocol <NSObject>
2
​
3
@optional
4
/** 每发现一个蓝牙设备调用一次 */
5
- (void)didDiscoverPeripheral:(CBPeripheral *)peripheral RSSI:(NSNumber *)RSSI;
6
​
7
/** 成功连接 BLE 或者观察通知:PTDispatchModeChangeToBLE */
8
- (void)didConnectPeripheral:(CBPeripheral *)peripheral;
9
​
10
/** 成功连接 WiFi 或者观察通知:PTDispatchModeChangeToWIFI */
11
- (void)didConnectWiFi:(NSString *)ip;

蓝牙4.0(低功耗蓝牙、BLE):

BLE 模式的特点:

  1. 功耗低。
  2. 连接速度很快,连接距离远(100米)。
  3. 相应的传输数据量小,比传统蓝牙传输的小很多,单次约能发送20-127字节。

综上,BLE 模式适合用来做遥控,遥控 LED 灯,窗帘,智能电视之类。智能手环和手机的数据同步,防丢器之类的东西。BLE 模式并不适合用来做较大数据量的传输使用。

通讯流程

BLE通讯流程:

1、创建 CBCentralManager 中心对象

2、central扫描外设 discover

3、central连接外设 connect

4、扫描外设中的服务和特征discover

  • 获取外设的 services:
  • 执行:discoverServices
  • 成功后执行:peripheral:didDiscoverServices委托方法
  • 获取外设的 Characteristics,获取 Characteristics 的值:
  • 执行discoverCharacteristics:forService
  • 成功后执行:peripheral:didDiscoverCharacteristicsForService:error委托方法
  • 获取外设的Characteristics 的 Descriptor 和 Descriptor 的值:
  • 读特征值:readValueForCharacteristic
  • 读到后进入:didUpdateValueForCharacteristic:error委托方法
  • 搜索 Characteristic:discoverDescriptorsForCharacteristic
  • 搜到后进入:didDiscoverDescriptorsForCharacteristic:error委托方法
  • 获取到 Descriptors 的值:peripheral:didUpdateValueForDescriptor:error

Descriptors 是对 characteristic 的描述,一般是字符串

  • 把数据写到 Characteristic:writeCharacteristic:charactericstic:value
  • 读 RSSI,用通知的方式订阅数据等。

5、与外设做数据交互 explored interact

6、订阅 Characteristic 的通知:notifyCharacteristic:characteristic

取消通知:cancelNotifyCharacteristic:characteristic

7、断开连接 disconnect:disconnnectPeripheral:peripheral

WiFi 通讯流程:

1、创建 AsyncSocket 对象

2、在 iOS 系统设置中连接 WiFi

3、获取当前 WiFi 的路由 IP 地址

4、连接当前 WiFi 的 IP,端口为9001:connectToHost:onPort:withTimeout:error:

5、成功连接打印机 WiFi,onSocket:didConnectToHost:port:代理方法

6、断开打印机 WiFi,onSocketDidDisconnect:代理方法

7、向打印机 WiFi 发送数据:writeData:withTimeout:tag: